using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using CPlatePrintSys.Domain.Models;
using Prism.Mvvm;

namespace CPlatePrintSys.Win.ViewModels
{
    /// <summary>
    /// 工作空间节点ViewModel，用于UI绑定
    /// </summary>
    public class WorkSpaceNodeViewModel : BindableBase
    {
        private readonly WorkSpaceNode _workSpaceNode;
        private bool _isSelected;
        private string _name;
        private List<WorkSpaceNodeViewModel> _children;

        public WorkSpaceNodeViewModel(WorkSpaceNode workSpaceNode)
        {
            _workSpaceNode = workSpaceNode;
            _name = workSpaceNode.Name;
            _isSelected = workSpaceNode.IsSelected;
            
            // 初始化子节点
            if (workSpaceNode.Children != null && workSpaceNode.Children.Any())
            {
                _children = workSpaceNode.Children.Select(child => new WorkSpaceNodeViewModel(child)).ToList();
            }
            else
            {
                _children = new List<WorkSpaceNodeViewModel>();
            }
        }

        public int Id => _workSpaceNode.Id;
        public int WorkSpaceId => _workSpaceNode.WorkSpaceId;
        public string NodeType => _workSpaceNode.NodeType;
        public string NodeValue => _workSpaceNode.NodeValue;
        public int? ParentNodeId => _workSpaceNode.ParentNodeId;
        public int SortOrder => _workSpaceNode.SortOrder;

        public string Name
        {
            get => _name;
            set
            {
                SetProperty(ref _name, value);
                _workSpaceNode.Name = value;
            }
        }

        public bool IsSelected
        {
            get => _isSelected;
            set
            {
                SetProperty(ref _isSelected, value);
                _workSpaceNode.IsSelected = value;
            }
        }

        public List<WorkSpaceNodeViewModel> Children
        {
            get => _children;
            set
            {
                SetProperty(ref _children, value);
                if (value != null)
                {
                    _workSpaceNode.Children = value.Select(vm => vm._workSpaceNode).ToList();
                }
            }
        }

        public WorkSpaceNodeViewModel Parent { get; set; }

        /// <summary>
        /// 获取原始的工作空间节点
        /// </summary>
        public WorkSpaceNode GetWorkSpaceNode()
        {
            return _workSpaceNode;
        }

        /// <summary>
        /// 从ViewModel创建WorkSpaceNode
        /// </summary>
        public static WorkSpaceNode CreateWorkSpaceNode(WorkSpaceNodeViewModel viewModel)
        {
            var node = new WorkSpaceNode
            {
                Id = viewModel.Id,
                WorkSpaceId = viewModel.WorkSpaceId,
                Name = viewModel.Name,
                NodeType = viewModel.NodeType,
                NodeValue = viewModel.NodeValue,
                ParentNodeId = viewModel.ParentNodeId,
                SortOrder = viewModel.SortOrder,
                IsSelected = viewModel.IsSelected
            };

            if (viewModel.Children != null && viewModel.Children.Any())
            {
                node.Children = viewModel.Children.Select(CreateWorkSpaceNode).ToList();
            }

            return node;
        }

        /// <summary>
        /// 从WorkSpaceNode列表创建ViewModel列表
        /// </summary>
        public static List<WorkSpaceNodeViewModel> CreateFromWorkSpaceNodes(List<WorkSpaceNode> nodes)
        {
            if (nodes == null) return new List<WorkSpaceNodeViewModel>();
            
            return nodes.Select(node => new WorkSpaceNodeViewModel(node)).ToList();
        }

        /// <summary>
        /// 从ViewModel列表创建WorkSpaceNode列表
        /// </summary>
        public static List<WorkSpaceNode> CreateWorkSpaceNodes(List<WorkSpaceNodeViewModel> viewModels)
        {
            if (viewModels == null) return new List<WorkSpaceNode>();
            
            return viewModels.Select(CreateWorkSpaceNode).ToList();
        }
    }
} 